import React from 'react';

export interface TableColumn<T = any> {
  key: string;
  title: React.ReactNode;
  dataIndex: string;
  width?: number | string;
  fixed?: 'left' | 'right' | boolean;
  align?: 'left' | 'center' | 'right';
  sorter?: boolean | ((a: any, b: any) => number);
  sortOrder?: 'ascend' | 'descend' | null;
  filters?: Array<{ text: string; value: any }>;
  filterMultiple?: boolean;
  filteredValue?: any[];
  render?: (value: any, record: T, index: number) => React.ReactNode;
  ellipsis?: boolean;
  hidden?: boolean;
  description?: string;
}

export interface TablePagination {
  current: number;
  pageSize: number;
  total: number;
  showSizeChanger?: boolean;
  showQuickJumper?: boolean;
  showTotal?: (total: number, range: [number, number]) => React.ReactNode;
  pageSizeOptions?: string[];
  onChange?: (page: number, pageSize: number) => void;
}

export interface TableSearchField {
  key: string;
  label: string;
  placeholder?: string;
  type: 'input' | 'select' | 'date' | 'number' | 'treeSelect';
  options?: Array<{ label: string; value: any }>;
  allowClear?: boolean;
}

export interface TableProps<T = any> {
  columns: TableColumn<T>[];
  dataSource: T[];
  loading?: boolean;
  rowKey?: string | ((record: T) => string);
  rowSelection?: {
    selectedRowKeys?: React.Key[];
    onChange?: (selectedRowKeys: React.Key[], selectedRows: T[]) => void;
    getCheckboxProps?: (record: T) => any;
    type?: 'checkbox' | 'radio';
  };
  pagination?: false | TablePagination;
  bordered?: boolean;
  size?: 'small' | 'middle' | 'large';
  showHeader?: boolean;
  scroll?: { x?: number | string; y?: number | string };
  title?: () => React.ReactNode;
  footer?: () => React.ReactNode;
  expandable?: {
    expandedRowKeys?: React.Key[];
    onExpandedRowsChange?: (expandedRowKeys: React.Key[]) => void;
    onExpand?: (expanded: boolean, record: T) => void;
    expandRowByClick?: boolean;
    rowExpandable?: (record: T) => boolean;
    expandedRowRender: (record: T, index: number, indent: number, expanded: boolean) => React.ReactNode;
  };
  searchFields?: TableSearchField[];
  onSearch?: (params: Record<string, any>) => void;
  onExport?: (format: 'csv' | 'excel' | 'pdf') => void;
  batchActions?: Array<{
    key: string;
    label: string;
    icon?: React.ReactNode;
    danger?: boolean;
    onClick: (selectedRows: T[]) => void;
  }>;
  columnSettings?: {
    enabled?: boolean;
    defaultVisibleColumns?: string[];
    onColumnsChange?: (visibleColumns: string[]) => void;
  };
  refresh?: () => void;
  extra?: React.ReactNode;
  rowClassName?: (record: T, index: number) => string;
  onRow?: (record: T, index: number) => any;
  onChange?: (pagination: TablePagination, filters: Record<string, any>, sorter: any) => void;

  // 新增配置选项
  stickySettings?: {
    enabled?: boolean;
    header?: boolean; // 固定表头
    search?: boolean; // 固定查询条件
    offsetTop?: number; // 距离顶部的偏移量
    onHeaderChange?: (enabled: boolean) => void; // 固定表头变化回调
  };
  columnPinning?: {
    enabled?: boolean;
    defaultPinnedColumns?: {
      left?: string[];
      right?: string[];
    };
    onPinnedColumnsChange?: (pinnedColumns: { left?: string[]; right?: string[] }) => void;
  };
}

export interface TableState {
  searchText: string;
  searchParams: Record<string, any>;
  filteredInfo: Record<string, any>;
  sortedInfo: any;
  selectedRowKeys: React.Key[];
  selectedRows: any[];
  visibleColumns: string[];
  columnSettingsVisible: boolean;
  pinnedColumns: {
    left?: string[];
    right?: string[];
  };
  columnPinningVisible: boolean;
}

export type TableInstance = {
  refresh: () => void;
  resetSearch: () => void;
  getSelectedRows: () => any[];
  getSearchParams: () => Record<string, any>;
};